package jm.algorithm.dp;

/**
 * @Description dp - 最大子序列和
 * @date 2022/5/23 20:42
 */
public class MaxSubArray {
    public static void main(String[] args) {
        int[] arr = {-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(maxSubArray2(arr));
    }

    /**
     *  dp 求最大连续子序列和
     * @param nums 整体序列
     * @return
     */
    static int maxSubArray(int[] nums){
        if (nums == null || nums.length == 0) return 0;
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        int max = dp[0];
        for (int i = 1; i < dp.length; i++) {
            if (dp[i - 1] <= 0){
                dp[i] = nums[i];
            }else {
                dp[i] = dp[i - 1] + nums[i];
            }
            max = Math.max(dp[i], max);
        }
        return max;
    }

    /**
     *  dp 求最大连续子序列和 - 变量优化
     * @param nums 整体序列
     * @return
     */
    static int maxSubArray2(int[] nums){
        if (nums == null || nums.length == 0) return 0;
        int dp = nums[0];
        int max = dp;
        for (int i = 1; i < nums.length; i++) {
            if (dp <= 0){
                dp = nums[i];
            }else {
                dp = dp + nums[i];
            }
            max = Math.max(dp, max);
        }
        return max;
    }
}
